package com.qf.card.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.card.dao.TDealMapper;
import com.qf.card.pojo.TDeal;
import com.qf.card.service.TDealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/****
 * @Author:shenkunlin
 * @Description:TDeal业务层接口实现类
 * @Date 2019/6/14 0:16
 *****/
@Service
public class TDealServiceImpl implements TDealService {

    @Autowired
    private TDealMapper tDealMapper;


    /**
     * TDeal条件+分页查询
     * @param tDeal 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public PageInfo<TDeal> findPage(TDeal tDeal, int page, int size){
        //分页
        PageHelper.startPage(page,size);
        //搜索条件构建
        Example example = createExample(tDeal);
        //执行搜索
        return new PageInfo<TDeal>(tDealMapper.selectByExample(example));
    }

    /**
     * TDeal分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<TDeal> findPage(int page, int size){
        //静态分页
        PageHelper.startPage(page,size);
        //分页查询
        return new PageInfo<TDeal>(tDealMapper.selectAll());
    }

    /**
     * TDeal条件查询
     * @param tDeal
     * @return
     */
    @Override
    public List<TDeal> findList(TDeal tDeal){
        //构建查询条件
        Example example = createExample(tDeal);
        //根据构建的条件查询数据
        return tDealMapper.selectByExample(example);
    }


    /**
     * TDeal构建查询对象
     * @param tDeal
     * @return
     */
    public Example createExample(TDeal tDeal){
        Example example=new Example(TDeal.class);
        Example.Criteria criteria = example.createCriteria();
        if(tDeal!=null){
            // 明细表id
            if(!StringUtils.isEmpty(tDeal.getPkDealId())){
                    criteria.andEqualTo("pkDealId",tDeal.getPkDealId());
            }
            // 交易明细详情表id
            if(!StringUtils.isEmpty(tDeal.getUkDpId())){
                    criteria.andEqualTo("ukDpId",tDeal.getUkDpId());
            }
            // 收/付款人的名字(对方信息)
            if(!StringUtils.isEmpty(tDeal.getReciprocalName())){
                    criteria.andEqualTo("reciprocalName",tDeal.getReciprocalName());
            }
            // 收/付款人的账号(对方信息)
            if(!StringUtils.isEmpty(tDeal.getReciprocalAccount())){
                    criteria.andEqualTo("reciprocalAccount",tDeal.getReciprocalAccount());
            }
            // 交易金额
            if(!StringUtils.isEmpty(tDeal.getTradingAmount())){
                    criteria.andEqualTo("tradingAmount",tDeal.getTradingAmount());
            }
            // 转入:1/支出0
            if(!StringUtils.isEmpty(tDeal.getOutputInput())){
                    criteria.andEqualTo("outputInput",tDeal.getOutputInput());
            }
            // 交易日期
            if(!StringUtils.isEmpty(tDeal.getTradingHour())){
                    criteria.andEqualTo("tradingHour",tDeal.getTradingHour());
            }
        }
        return example;
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(Integer id){
        tDealMapper.deleteByPrimaryKey(id);
    }

    /**
     * 修改TDeal
     * @param tDeal
     */
    @Override
    public void update(TDeal tDeal){
        tDealMapper.updateByPrimaryKey(tDeal);
    }

    /**
     * 增加TDeal
     * @param tDeal
     */
    @Override
    public void add(TDeal tDeal){
        tDealMapper.insert(tDeal);
    }

    /**
     * 根据ID查询TDeal
     * @param id
     * @return
     */
    @Override
    public TDeal findById(Integer id){
        return  tDealMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询TDeal全部数据
     * @return
     */
    @Override
    public List<TDeal> findAll() {
        return tDealMapper.selectAll();
    }
}
